using BackOffice.Application.Services;
using Backoffice.Domain.Repositories;
using Backoffice.Domain.Entities.App;
using Microsoft.AspNetCore.Mvc;
using BackOffice.Application.DTOs;

namespace BackOffice.Api.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class MenuController : ControllerBase
    {
        private readonly IMenuService _menuService;
        private readonly IRepository<AppMenu> _menuRepo;

        public MenuController(IMenuService menuService, IRepository<AppMenu> menuRepo)
        {
            _menuService = menuService;
            _menuRepo = menuRepo;
        }

        // 获取菜单树
        [HttpGet("tree")]
        public async Task<IActionResult> GetMenuTree()
        {
            var result = await _menuService.GetMenuTreeAsync();
            return Ok(result.Data);
        }

        // 创建菜单
        [HttpPost("create")]
        public async Task<IActionResult> Create([FromBody] CreateMenuDto dto)
        {
            var result = await _menuService.CreateMenuAsync(dto);
            return Ok(result.Data);
        }

        // 更新菜单
        [HttpPut("update/{menuId}")]
        public async Task<IActionResult> Update(Guid menuId, [FromBody] UpdateMenuDto dto)
        {
            var result = await _menuService.UpdateMenuAsync(menuId, dto);
            return Ok(result.Data);
        }

        // 删除菜单
        [HttpDelete("delete/{menuId}")]
        public async Task<IActionResult> Delete(Guid menuId)
        {
            var result = await _menuService.DeleteMenuAsync(menuId);
            return Ok(result.Data);
        }

        // 移动菜单
        [HttpPost("move/{menuId}")]
        public async Task<IActionResult> MoveMenu(Guid menuId, [FromBody] MoveMenuDto dto)
        {
            var result = await _menuService.MoveMenuAsync(menuId, dto.NewParentId, dto.NewOrder);
            return Ok(result.Data);
        }
    }
}